/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2012-2016 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "fvCFD.H"

#include "volFields.H"
#include "fvcGrad.H"

// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

template<class Type>
void Foam::functionObjects::ss_T_1Budget::processField
(
    const word& fieldName,
    const tmp<GeometricField<Type, fvPatchField, volMesh>>& tvalue
)
{
    typedef GeometricField<Type, fvPatchField, volMesh> FieldType;

    const word scopedName = word("ss_T_1_") + fieldName;

    if (obr_.foundObject<FieldType>(scopedName))
    {
        FieldType& fld =
            const_cast<FieldType&>(obr_.lookupObject<FieldType>(scopedName));
        fld == tvalue();
    }
    else if (obr_.found(scopedName))
    {
        WarningInFunction
            << "Cannot store turbulence field " << scopedName
            << " since an object with that name already exists"
            << nl << endl;
    }
    else
    {
      obr_.store
        (
            new FieldType
            (
                IOobject
                (
                    scopedName,
                    obr_.time().timeName(),
                    obr_,
                    IOobject::READ_IF_PRESENT,
                    IOobject::NO_WRITE
                ),
                tvalue
            )
        );
    }
}
//2021.4.29

// returns the production term 产生项P_k
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::ss_T_1Budget::Pk() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  const volVectorField UPrime = U - UMean;
  
  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;

  
  const volVectorField T_1Prime_UPrime(T_1Prime * UPrime);
  const volVectorField T_1Mean_grd = fvc::grad( T_1Mean );
  const volScalarField R_P = (T_1Prime_UPrime & T_1Mean_grd);
  

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Pk",
	      R_P.mesh().time().timeName(),
	      R_P.mesh()
	   ),
	  (-2.0)*R_P,
	  R_P.boundaryField().types()
       )
   );
}


// returns the viscous diffusion term 1
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::ss_T_1Budget::Dk_1() const
{

  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;

  const volScalarField R_1 = (nu_ss_T_1 / Pr_1) * (fvc::laplacian(T_1Prime));

  

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Dk_1",
	      R_1.mesh().time().timeName(),
	      R_1.mesh()
	   ),
	  R_1, 
	  R_1.boundaryField().types()
       )
   );
}


// returns the viscous diffusion term 2
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::ss_T_1Budget::Dk_2() const
{

  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;

  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  const volVectorField UPrime = U - UMean;

  const volScalarField R_2 = (UPrime & fvc::grad(T_1Prime * T_1Prime));

   

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Dk_2",
	      R_2.mesh().time().timeName(),
	      R_2.mesh()
	   ),
	  R_2, 
	  R_2.boundaryField().types()
       )
   );
}


// returns the dissipation term 耗散项 Epsilon
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::ss_T_1Budget::Epik() const
{ 
  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;
  const volVectorField T_1Prime_grd = (fvc::grad(T_1Prime));
  
  const volScalarField R_Epi =( nu_ss_T_1 / Pr_1 )*(T_1Prime_grd & T_1Prime_grd);
  
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Epik",
	      R_Epi.mesh().time().timeName(),
	      R_Epi.mesh()
	   ),
	  (-2.0)*R_Epi,
	  R_Epi.boundaryField().types()
       )
   );
}


// ************************************************************************* //
